//
//  MyViewController.m
//  智慧小区
//
//  Created by 高永杨 on 2017/3/16.
//  Copyright © 2017年 zhongchuang. All rights reserved.
//

#import "MyViewController.h"
#import "headV.h"
#import "IdentiPersonVC.h"
#import "AdressViewController.h"
#import "SetViewController.h"
#import "RSKImageCropper.h"
#import "RegisterClassController.h"
#import "OpinionsVC.h"
#import "LovingHeartVC.h"
#import "OpinionsList.h"
@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource,headVDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,RSKImageCropViewControllerDelegate,RSKImageCropViewControllerDataSource>
//
/**<#参数意思#>*/
@property(nonatomic,strong)NSMutableArray *imageArr;
/**<#参数意思#>*/
@property(nonatomic,strong)NSMutableArray *textArr;
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UITableView *tableV;

@end

@implementation MyViewController
-(NSMutableArray *)imageArr
{
    if (!_imageArr) {
        _imageArr = [NSMutableArray arrayWithObjects:@"address_ico",@"baoxiuL_icon",@"huzhu_icon",@"fanchan_icon",@"fangke_icon",@"yijianjianyi_icon",@"shehzi_icon",nil];
    }
    return _imageArr;
}
-(NSMutableArray *)textArr
{
    if (!_textArr) {
        _textArr = [NSMutableArray arrayWithObjects:@"收获地址",@"报修记录",@"互助记录",@"我的房产",@"访客管理",@"意见建议",@"设置", nil];
    }
    return _textArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航.png"] forBarMetrics:UIBarMetricsDefault];
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.navigationItem.title = @"智慧出行";
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = attrs;
    headV *headVie = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([headV class]) owner:self options:nil].firstObject;
    headVie.tag = 1000;
    headVie.delegate = self;
    headVie.frame = CGRectMake(0, 0, GyyScreenW, GyyScreenW*0.6);//0.6
    pesonMole *person = [[pesonMole alloc]init];
    [headVie sendMoDel:person];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, GyyScreenW, GyyScreenW*0.6)];
    [view addSubview:headVie];
    self.tableV.tableHeaderView = view;
    [self.view layoutIfNeeded];
//    headVie.height = 370;
//    self.tableV.tableHeaderView.height = 370;
//    self.headView.frame = CGRectMake(0, 0, GyyScreenW, 150);
//    self.tableV.tableHeaderView = self.headView;
    // Do any additional setup after loading the view from its nib.
}


-(void)viewDidAppear:(BOOL)animated
{
    GYYLog(@"------%f",self.tableV.tableHeaderView.height);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{


}

-(void)sendTag:(NSInteger)tag
{
    switch (tag) {
        case 100:
            
            break;
        case 200:
            
            break;
        case 300:
            
            break;
        default:
            break;
    }
    NSLog(@"-------%ld",(long)tag);
    
}
-(void)xiuGaiTuXiang
{
    UIAlertController *aletVie = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *alert1 = [UIAlertAction actionWithTitle:@"从相册选取照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self create:0];
    }];
    UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self create:1];
    }];
    UIAlertAction *alert3 = [UIAlertAction actionWithTitle:@"查看原图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [self showDaImage];
    }];
    UIAlertAction *alert4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [aletVie addAction:alert1];
    [aletVie addAction:alert2];
//  [aletVie addAction:alert3];
    [aletVie addAction:alert4];
    [self presentViewController:aletVie animated:YES completion:nil];
}
-(void)create:(NSInteger )ta{
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = ta;
    [self presentViewController:imagePickerController animated:YES completion:^{

    }];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //    SaveViewC *viewC = [[SaveViewC alloc]init];
    //    viewC.delegate = self;
    //    viewC.imaged = image;
    //    viewC.view.frame = CGRectMake(0, 0, GyyScreenW, GyyScreenH);
    //    [self.navigationController pushViewController:viewC animated:YES];
    RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:image cropMode:RSKImageCropModeCustom];
    imageCropVC.delegate = self;
    imageCropVC.dataSource = self;
    [self.navigationController pushViewController:imageCropVC animated:YES];
}
- (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect
{
    [self.navigationController popViewControllerAnimated:YES];

    [SVProgressHUD showWithStatus:@"正在上传中"];
   

    NSMutableDictionary *dictiong = [NSMutableDictionary dictionary];
    [dictiong setValue:@"e4022b7ca0e3f31048c3c4ac30ae3967" forKey:@"token"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:GolobaUrl parameters:dictiong constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *data1 = UIImageJPEGRepresentation(croppedImage, 0);
        [formData appendPartWithFileData:data1 name:@"file" fileName:@"haha.jpg" mimeType:@"image/jpeg"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        GYYLog(@"------%@",responseObject[@"message"]);
        if (responseObject[@"error"]) {
            [SVProgressHUD showSuccessWithStatus:@"上传成功"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            headV *headView = [self.view viewWithTag:1000];
            headView.headImageV.image = croppedImage;
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:@"上传失败,请重新上传"];
        }
        NSLog(@"----%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"-------%@",error);
        [SVProgressHUD showErrorWithStatus:@"上传失败,请检查网络"];
    }];
}
- (CGRect)imageCropViewControllerCustomMaskRect:(RSKImageCropViewController *)controller

{
    
    //返回图片的位置
    
    return CGRectMake(GyyScreenW/2-100, GyyScreenH/2-100, 200, 200);
    
}

- (UIBezierPath *)imageCropViewControllerCustomMaskPath:(RSKImageCropViewController *)controller

{
    
    //返回裁剪框的位置
    
    UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(GyyScreenW/2-100, GyyScreenH/2-100, 200, 200) cornerRadius:100];
    
    return path;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cellStr";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellStr"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.imageView.image = [UIImage imageNamed:self.imageArr[indexPath.row]];
    cell.textLabel.text = self.textArr[indexPath.row];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"333333"];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imageArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return GyyScreenW*0.12;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYYLog(@"-------%f",tableView.tableHeaderView.height);
    switch (indexPath.row) {
        case 0:
            [self setJumpToAdress];
            break;
        case 2:
            [self jumpToLoveHeart:HelpOther];
            break;
        case 5:
            [self jumpToOpinions];
            break;
        case 6:
            [self setJumpTo];
            break;
        default:
            break;
    }
}
-(void)jumpToOpinions
{
   // OpinionsVC *opinio = [[OpinionsVC alloc]init];
    //[self.navigationController pushViewController:opinio animated:YES];
    OpinionsList *opionList = [[OpinionsList alloc]init];
    [self.navigationController pushViewController:opionList animated:YES];
}
-(void)setJumpToAdress
{
    AdressViewController *addres = [[AdressViewController alloc]init];
    [self.navigationController pushViewController:addres animated:YES];
//    RegisterClassController *registerClass = [[RegisterClassController alloc]init];
//    [self.navigationController pushViewController:registerClass animated:YES];
}
//跳转到设置页面
-(void)setJumpTo
{
    SetViewController *setV = [[SetViewController alloc]init];
    [self.navigationController pushViewController:setV animated:YES];
}
//跳到互助记录
-(void)jumpToLoveHeart:(CitiHelpType)type
{
    LovingHeartVC *loving = [[LovingHeartVC alloc]init];
    loving.type = type;
    [self.navigationController pushViewController:loving animated:YES];
}

-(void)identityPerson
{
    IdentiPersonVC *identVC = [[IdentiPersonVC alloc]init];
    [self.navigationController pushViewController:identVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
